package cn.com.cfae.iras.doc.analyze.parse.tokenizer;

import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import com.hankcs.hanlp.corpus.document.sentence.Sentence;
import com.hankcs.hanlp.corpus.document.sentence.word.IWord;
import com.hankcs.hanlp.tokenizer.NLPTokenizer;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class XJLLTokenizer extends BaseTokenizer {


    public XJLLTokenizer() {
        NLPTokenizer.ANALYZER.enableCustomDictionaryForcing(true);
        NLPTokenizer.ANALYZER.enableNumberQuantifierRecognize(true);
    }

    private static Logger logger = LoggerFactory.getLogger(XJLLTokenizer.class);

    public void splitWords(ExtractBizModel extractBizModel, String text) {
        logger.info("开始进行资产负债表分词......");
        long t1 = System.currentTimeMillis();


        Sentence sentence = NLPTokenizer.ANALYZER.analyze(text);

        ExtractItem extractItem = null;
        List<IWord> wordList = sentence.wordList;
        IWord iWord = null;
        IWord targetIWord = null;
        int offset = -1;
        for (int i = 0; i < wordList.size(); i++) {
            iWord = wordList.get(i);
            String word = iWord.getValue();
            logger.info("名词：{}，词性：{}。", word, iWord.getLabel());
            if (extractBizModel.getSynonyms().contains(word)) {
                offset = i;
                targetIWord = iWord;
                extractItem = extractBizModel.getExtractItemBySynonym(word);
                logger.info("解析到资产负债表会计科目，科目名称：{}，ItemName：{}，索引位置：{}。", word, extractItem.getItemName(), offset);
                break;
            }
        }

        for (int i = 0; i < offset; i++) {
            iWord = wordList.get(i);
            logger.info("判断上文：{}。", iWord.getValue());
            if (isJSNJYQ(iWord.getValue())) {
                //TODO 后续加逻辑
                logger.info("解析到近三年及一期，添加Key值：{}。", Arrays.asList(getYears()));
                for (String year : getYears()) {
                    extractBizModel.addTable(year);
                }
                break;
            }
        }
        List<String> amtList = new ArrayList<String>();
        if (offset == -1) {
            return;
        }
        for (int i = offset; i < wordList.size(); i++) {
            iWord = wordList.get(i);
            logger.info("判断下文：{}。", iWord.getValue());
            if (isZCFZ_JE(iWord.getValue().trim())) {
                logger.info("解析到对应金额：{}。", iWord.getValue());
                amtList.add(iWord.getValue());
            }
            if (amtList.size() == 4) {
                break;
            }
        }

        String[] years = getYears();
        for (int i = 0; i < amtList.size(); i++) {
            try {
                ExtractItem extractItem1 = (ExtractItem) BeanUtils.cloneBean(extractItem);
                KVText key = new KVText();
                key.setText(extractItem1.getSynonyms()[0]);
                KVText value = new KVText();
                value.setText(amtList.get(i));
                extractItem1.setKey(key);
                extractItem1.setValue(value);
                extractBizModel.getTable(years[i]).put(extractItem1.getItemName(), extractItem1);
            } catch (IllegalAccessException e) {
                logger.error(e.getMessage(), e);
            } catch (InstantiationException e) {
                logger.error(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                logger.error(e.getMessage(), e);
            } catch (NoSuchMethodException e) {
                logger.error(e.getMessage(), e);
            }

        }
        long t2 = System.currentTimeMillis();
        logger.info("资产负债表分词完成，耗时：{}毫秒。", (t2 - t1));
    }


    private String[] getYears() {
        return new String[]{"2015", "2016", "2017", "2018"};
    }

}
